﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TugasAkhir.Models;

namespace TugasAkhir
{

    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();
        }

        protected void btSubmit_Click(object sender, EventArgs e)
        {
            string idRole = string.Empty;
            string username = string.Empty;

            if (PenggunaModel.Masuk(this.txtUsername.Text, this.txtPassword.Text, out username, out idRole))
            {
                Session["id_role"] = idRole;
                Session["username"] = username;
                CheckSession();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowScript", "$('#gagalModal').modal('show');", true);
            }
        }

        private void CheckSession()
        {
            if (Session["id_role"] != null && Session["username"] != null)
            {
                if (Session["id_role"].ToString() == "RL01")
                {
                    Response.Redirect("/Mahasiswa.aspx");
                }
                else if (Session["id_role"].ToString().StartsWith("RL") && Session["id_role"].ToString() != "RL01")
                {
                    Response.Redirect("/Dosen.aspx");
                }
            }
        }
    }
}